package com.tqw.item.controller;

import com.tqw.entity.JWTUtil;
import com.tqw.entity.Result;
import com.tqw.item.service.FavoritesService;
import com.tqw.pojo.Item;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

import static com.tqw.entity.StatusCode.*;

/**
 * @description: some desc
 * @author: Administrator
 * @date: 2022/4/16 16:35
 */
@RestController
@RequestMapping("/item/favorites")
public class FavoriteController {

    @Autowired
    private FavoritesService favoritesService;

    /**
     * 商品详情收藏夹收藏与取消
     * 请求类型post
     * 请求路径 URL：http://localhost:8080/#/commoditys/favorites
     * 返回参数Result
     * @param item
     * @return
     */
    @PostMapping("")
    public Result favorites(@RequestHeader("Authorization") String token, @RequestBody Item item){
        Integer code=favoritesService.findToken(token,item.getId());
        if (code==null){
            return new Result(false, ERROR, "该商品已售出或下架");
        }
        if (!JWTUtil.isExpired(token)){
            try {
                Map<String,String> map=favoritesService.updateFavorite(token,item.getId());
                if (map!=null){
                    return new Result<Map<String,String>>(true,OK,"查询成功",map);
                }
                return new Result(false, REPERROR, "该商品为本人商品");
            } catch (Exception e) {
                return new Result(false, TOKEN_EXPIRED, "token数据不匹配");
            }
        }
        return new Result(false, TOKEN_EXPIRED, "token数据不匹配");

    }
}
